import {useStore} from "vuex";
import {TodoItem, TodoStatus} from "@/TodoPage/DataTypes";
import {ADD_TODO, DELETE_STATUS, UPDATE_STATUS} from "@/store";

/**
 * @Description :
 * @author : xyg
 * @date : 2021-05-28 13:40
 */


export interface IUseTodo {
    add: (content: string, callback: () => void | undefined) => void,
    update: (item: TodoItem) => void,
    getTagType: (row: TodoItem) => string,
    getList: () => TodoItem[],
    delById: (id: number) => void,
}


export function useTodo(): IUseTodo {

    const store = useStore()


    function add(content: string, callback: () => void | undefined): void {

        const item: TodoItem = {
            id: new Date().getTime(),
            content: content,
            status: TodoStatus.WILL
        }
        store.dispatch(ADD_TODO, item).then(callback)
    }

    function update(item: TodoItem): void {
        store.dispatch(UPDATE_STATUS, item).then(null)
    }

    function getTagType(row: TodoItem): string {
        switch (row.status) {
            case TodoStatus.FINISH:
                return "success";
            case TodoStatus.DOING:
                return "";
            case TodoStatus.WILL:
                return "info";
            default:
                return "info"
        }
    }

    function getList(): TodoItem[] {
        return store.state.toDoList;
    }

    function delById(id: number): void {

        store.dispatch(DELETE_STATUS, id).then(null)
    }


    return {
        add, update, getTagType, getList, delById
    }
}

